package com.aijk.web.admin.controller.openapi;

import com.aijk.commons.mijo.utils.PojoUtil;
import com.aijk.ehm.api.EhmCompanyApi;
import com.aijk.ehm.api.EhmPeopleCrowdApi;
import com.aijk.ehm.api.EhmPeopleCrowdMutexApi;
import com.aijk.ehm.model.EhmPeopleCrowd;
import com.aijk.ehm.model.EhmPeopleCrowdMutex;
import com.aijk.ehm.model.dto.EhmPeopleCrowdDto;
import com.aijk.ehm.model.qo.EhmCompanyQo;
import com.aijk.ehm.model.qo.EhmPeopleCrowdMutexQo;
import com.aijk.ehm.model.qo.EhmPeopleCrowdQo;
import com.aijk.web.admin.controller.openapi.entity.ApiResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.*;

/**
 * 人群管理
 */
@Component
@Controller
public class EhmCrowdController extends ApiBaseController {

    private static final Logger logger = LoggerFactory.getLogger(EhmCrowdController.class);

    @Autowired
    private EhmPeopleCrowdApi crowdApi;

    @Autowired
    private EhmPeopleCrowdMutexApi crowdMutexApi;
    /**
     * 人群获取
     * @return
     */
    @RequestMapping(path = "app/crowd/list", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Object companyList() {
        List<EhmPeopleCrowdDto> crowdDtos = new ArrayList<>();
        List<EhmPeopleCrowd> crowds = crowdApi.list(new EhmPeopleCrowdQo());
        List<EhmPeopleCrowdMutex> crowdMutexes = crowdMutexApi.list(new EhmPeopleCrowdMutexQo());

        if(crowds == null || crowds.size() == 0){
            return new ApiResponse("0" , "暂无数据");
        }

        crowdDtos = PojoUtil.convert(crowds,EhmPeopleCrowdDto.class);

        Map<Long,List<Long>> MutexeMap =new HashMap<>();
        if(crowdMutexes !=null && crowdMutexes.size() > 0){
            for (EhmPeopleCrowdMutex mutex:crowdMutexes){
                if(MutexeMap.containsKey(mutex.getPid())){
                    MutexeMap.get(mutex.getPid()).add(mutex.getHpid());
                }else{
                    MutexeMap.put(mutex.getPid(),new ArrayList<Long>(){{add(mutex.getHpid());}});
                }
            }
        }
        for (EhmPeopleCrowdDto dto : crowdDtos){
            if(MutexeMap.containsKey(dto.getId()))
                dto.setMutexeIds(MutexeMap.get(dto.getId()));
        }

        ApiResponse response = new ApiResponse();
        response.setData(crowdDtos);


//        Map<Long,List<Long>> MutexeMap =new HashMap<>();
//        for (EhmPeopleCrowdMutex mutex:crowdMutexes){
//            if(MutexeMap.containsKey(mutex.getPid())){
//                MutexeMap.get(mutex.getPid()).add(mutex.getHpid());
//            }else{
//                MutexeMap.put(mutex.getPid(),new ArrayList<Long>(){{add(mutex.getHpid());}});
//            }
//        }
//        for (EhmPeopleCrowd crowd:crowds){
//            EhmPeopleCrowdDto crowdDto = new EhmPeopleCrowdDto();
//            BeanUtils.copyProperties(crowdDto,crowd);
//            if(MutexeMap.containsKey(crowd.getId()))crowdDto.setMutexeIds(MutexeMap.get(crowd.getId()));
//            crowdDtos.add(crowdDto);
//        }
//        ApiResponse response = new ApiResponse();
//        response.setData(crowdDtos);
        return response;
    }


}
